package com.wechar.pub_account.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wechar.pub_account.entity.Button;
import com.wechar.pub_account.entity.Constant;
import com.wechar.pub_account.entity.Menu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WeCharUtil {

    static private Logger logger = LoggerFactory.getLogger(WeCharUtil.class);

    public static String getAccessToken() throws Exception{
        String path = Constant.TOKEN_URL.replace("APPID", Constant.APP_ID).replace("SECRET", Constant.APP_SECRET);
        String response = HttpUtil.openUrl(path, HttpUtil.GET);
        JSONObject json = JSONObject.parseObject(response);
        return json.get("access_token").toString();
    }

    //初始化菜单
    public static String initMenu() throws Exception{
        String access_token = getAccessToken();
        logger.info(access_token);
        String path = Constant.MENU_CREATE.replace("ACCESS_TOKEN", access_token);
        //初始化菜单
        Menu menu = new Menu();
        Button[] buttons = new Button[2];
        buttons[0] = new Button(Constant.BUTTON_CLICK, "今日歌曲", "V1001_TODAY_MUSIC");
        buttons[1] = new Button("菜单");
        Button[] son_buttons = new Button[2];
        son_buttons[0] = new Button(Constant.BUTTON_VIEW, "搜索", null, "http://www.soso.com/", null, null, null);
        son_buttons[1] = new Button(Constant.BUTTON_CLICK, "赞一下我们", "V1001_GOOD");
        buttons[1].setSub_button(son_buttons);
        menu.setButton(buttons);
        String menu_json = JSONArray.toJSONString(menu);
        logger.info(menu_json);
        return HttpUtil.openUrl(path, menu_json, HttpUtil.POST);
    }



}
